<?php
// 查询宠物信息
// header("content-type:text/html;charset=UTF-8");
include_once("conn.php");
include_once("d_getLoginInfo.php");
//设置中国时区
date_default_timezone_set('Asia/Shanghai');
// 获取当前日期
$getNowDate = date('Y-m-d');

if ($_GET['sendpetID'] != '') {
    $petID = $_GET['sendpetID'];
    $sql_petinfo = "SELECT * FROM pet_infos WHERE pet_ID = '" . $petID . "'";
    $result_petinfo = mysqli_query($connect_DB, $sql_petinfo);
    if ($result_petinfo) {
        if (mysqli_num_rows($result_petinfo) <= 0) {
            echo "<div class='error-tips'>查询宠物ID：<br>" . $petID . "<br>宠物信息不存在！<br>请核对后重试！</div>";
            exit();
        } else {
            $row_petinfo = mysqli_fetch_object($result_petinfo);
            if ($row_petinfo->pet_Birthday == '未填写') {
                $petage = '未知';
                $petbirthday = '主人未录入';
            } else {
                $petbirthday = $row_petinfo->pet_Birthday;
                $bir = new DateTime($row_petinfo->pet_Birthday);
                $now = new DateTime($getNowDate);
                $interval = $now->diff($bir);
                $petage = $interval->y . '岁' . $interval->m . '月';
            }

            if (substr($row_petinfo->pet_PhotoAddress, 0, 3) == 'def') {
                $petPhoto = "../public/pic/pet_photos/" . $row_petinfo->pet_PhotoAddress;
            } else {
                $petPhoto = "../public/pic/pet_photos/" . $row_petinfo->user_ID . "/" . $row_petinfo->pet_PhotoAddress;
            }
        }
    } else {
        echo "<div class='error-tips'>宠物信息查询失败！</div>";
        exit();
    }

    $sql_userinfo = "SELECT * FROM user_infos WHERE user_ID = '" . $row_petinfo->user_ID . "'";
    $result_userinfo = mysqli_query($connect_DB, $sql_userinfo);
    if ($result_userinfo) {
        $row_userinfo = mysqli_fetch_object($result_userinfo);
    } else {
        echo "<div class='error-tips'>用户资料查询失败！</div>";
        exit();
    }

    $sql_orderhistory = "SELECT * FROM order_history_infos WHERE pet_ID = '" . $row_petinfo->pet_ID . "'";
    $result_orderhistory = mysqli_query($connect_DB, $sql_orderhistory);
    if ($result_orderhistory) {
        $row_orderhistory = mysqli_num_rows($result_orderhistory);
    } else {
        echo "<div class='error-tips'>预约历史记录查询失败！</div>";
        exit();
    }

    $sql_order = "SELECT * FROM order_infos WHERE pet_ID = '" . $row_petinfo->pet_ID . "' AND orderDate >= '" . $getNowDate . "'";
    $result_order = mysqli_query($connect_DB, $sql_order);
    if ($result_order) {
        if (mysqli_num_rows($result_order) > 0) {
            $row_order = mysqli_num_rows($result_order) . "项";
        } else {
            $row_order = "暂无预约";
        }
    } else {
        echo "<div class='error-tips'>预约记录查询失败！</div>";
        exit();
    }

    $sql_disorder = "SELECT * FROM order_infos WHERE pet_ID = '" . $row_petinfo->pet_ID . "' AND orderDate < '" . $getNowDate . "'";
    $result_disorder = mysqli_query($connect_DB, $sql_disorder);
    if ($result_disorder) {
        if (mysqli_num_rows($result_disorder) > 0) {
            $row_disorder = "<b style='color:red;'>" . mysqli_num_rows($result_disorder) . "项</b>";
        } else {
            $row_disorder = "<b style='color:green;'>无信息</b>";
        }
    } else {
        echo "<div class='error-tips'>预约记录查询失败！</div>";
        exit();
    }

    echo "
    <div class='photo-div'>
        <img src='" . $petPhoto . "'>
    </div>
    <div class='name-age-div'>
        <p>名字：<i>" . $row_petinfo->pet_Name . "</i></p>
        <p>年龄：<i>" . $petage . "</i></p>
    </div>
    <div class='pet-info'>
        <p>宠物ID：<i>" . $row_petinfo->pet_ID . "</i></p>
        <p>宠物生日：<i>" . $petbirthday . "</i></p>
        <p>宠物分类：<i>" . $row_petinfo->pet_Type . "</i></p>
    </div>
    <div class='order-info'>
        <p>主人：<i>" . $row_userinfo->user_Name . "</i></p>
        <p>主人电话：<i>" . $row_userinfo->user_Phone . "</i></p>
        <p>宠物录入时间：<i>" . $row_petinfo->pet_AddDate . "</i></p>
        <p>宠物累计预约：<i>" . $row_orderhistory . "次</i></p>
        <p>违约预约：<i>" . $row_disorder . "</i></p>
        <p>宠物当前是否有预约：<i>" . $row_order . "</i></p>
    </div>
    ";
}

$connect_DB->close();
?>